#ifndef _TIMER_H
#define _TIMER_H

#include <pthread.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>  
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/timerfd.h>
#include <sys/time.h>
#include <iostream>

using namespace std;

typedef void(*PfnTimerHandlerCb)(void* pvCtx);

typedef struct TimerItem_
{
    int timerfd;
    PfnTimerHandlerCb func;
    void* user_ctx;
}TimerItem;

struct TagTimer
{
    int epollfd;
    int timer_num;
    TimerItem timer[128];
};

typedef struct TagTimer* pTimer;

#define EPOLL_LISTEN_CNT 256

namespace ddserver
{
namespace thread
{

class Timer
{
public:
    Timer();
    ~Timer();

    int start(unsigned int milli_time,int delay,PfnTimerHandlerCb cb,void* user_ctx);
    int add(unsigned int milli_time,int delay,PfnTimerHandlerCb cb,void* user_ctx);
    int stop();

private:
    void handleError();

private:
    pTimer m_pTimer;
};

}
}

#endif